/**
 * Created by Administrator on 2017/9/7.
 */

$(document).ready(function () {
    
    var productId=locationSearcher('productId');

    var redTpl=$('#red-product-template').html();
    var redCmp=Handlebars.compile(redTpl);

    $.ajax({
        url:C.interface.redIndex,
        type:'get',
        dataType:'json',
        data:{
            token:C.token,
            productId:productId
        },
        success:function (response) {
            if (response.result === 'success'){
                var data=response.data;
                console.log(data);
                $('#red-product-info').html(redCmp(data));
                //增加红包跳转链接
                $(".red-box-right").attr('ids',data.product.id);
                if($(".none-pull") != "立即领取"){
                	var id =  $(".red-box-right").attr('ids');
                	setTimeout(function () { 
                        location.href = "goodsDetail.html?productId="+id
                    }, 2000);
                }
                $('.now-pull').one('click',getRedPackage);
            }else if (response.result === 'login'){
                alert('登录过期请重新登录');
                againLogin();
            }else {
                alert(response.erroeMsg);
            }
        }
    });

    function getRedPackage() {
        $.ajax({
            url:C.interface.redGet,
            type:'get',
            dataType:'json',
            data:{
                productId:productId,
                token:C.token
            },
            success:function (response) {
                if (response.result === 'success'){
                    $('.now-pull').html('已领取');
                    $('.now-pull').addClass('none-pull').removeClass('now-pull').off('click');
                    //增加红包跳转链接
                  //  $(".red-box-right").attr('ids',data.product.id);
                    if($(".none-pull") != "立即领取"){
                    	var id =  $(".red-box-right").attr('ids');
                    	setTimeout(function () { 
                            location.href = "goodsDetail.html?productId="+id
                        }, 2000);
                    }
                }else {
                    alert('您已经领取过了');
                }
            }
        })
    }




});

